//
// Created by wxd on 2025/8/31.
// 全局的着色器管理器

#pragma once

#ifndef SHADERMANAGER_H
#define SHADERMANAGER_H

#include <mutex>
#include <memory>
#include <string>
#include <unordered_map>

#include "../glFramework/shader/Shader.h"

class ShaderManager {
public:
    static ShaderManager& getInstance() {
        static ShaderManager shaderManager;
        return shaderManager;
    }

    void addShader(const std::string& materialId, Shader* shader);
    bool contains(const std::string& materialId);
    bool removeShader(const std::string& materialId);
    Shader* getShader(const std::string& materialId);
    void clear();

private:
    ShaderManager() = default;
    ~ShaderManager() = default;

    std::mutex mLock {};
    std::unordered_map<std::string, Shader*> mSingleShaderMap{}; // 材质和着色器1:1

    bool isLocked() {
        std::unique_lock<std::mutex> lock(mLock, std::try_to_lock);
        return !lock.owns_lock();  // 如果获取锁失败，说明已经被锁定
    }
};

#endif //SHADERMANAGER_H
